<?php session_start(); //Session ?>
<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php include_once( "header.php" ); //Header ?>
<?php include_once( "footer.php" ); //Footer ?>
<?php
//include page header
header_template("Transaction", $_SERVER['PHP_SELF']."?dealid=".$_GET["dealid"]."&email=".$_GET["email"], "icon-tasks");

//db OBJECT invoked!
$db = new DATABASE();

//TMP DEFINES
define("SHOPNAME", "Schanaz");
?>
      <div class="row">
        <div class="span6">
<?php

  if (isset($_GET["dealid"]) && isset($_GET["email"])) {
    //Get Deal
    $cols = NULL;
    $conds = array();
    $conds[] = array(0);
    $conds[] = array(0);
    $conds[] = array($_GET["dealid"]);
    $query = $db->db_select("deal", $cols, $conds);
    $queries[] .= $query.";";
    $result = $db->database->query($query);
    $tmpDeal = $result->fetchAll(SQLITE_ASSOC);

    if (!empty($tmpDeal)) {

      //Get Customer
      $cols = NULL;
      $conds = array();
      $conds[] = array(0);
      $conds[] = array(0);
      $conds[] = array($tmpDeal[0]['customer_id']);
      $query = $db->db_select("customer", $cols, $conds);
      $queries[] .= $query.";";
      $result = $db->database->query($query);
      $tmpCust = $result->fetchAll(SQLITE_ASSOC);

      if ((!empty($tmpCust)) && ($_GET["email"] == $tmpCust[0]['customer_email'])) {
      
        //Get Bill
        $cols = NULL;
        $conds = array();
        $conds[] = array(15);
        $conds[] = array(0);
        $conds[] = array($tmpDeal[0]['deal_id']);
        $query = $db->db_select("bill", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpBill = $result->fetchAll(SQLITE_ASSOC);

        if (check_expire_deal($db, $tmpBill[0]['bill_id'])) {
          //Get Bill again!
          $cols = NULL;
          $conds = array();
          $conds[] = array(15);
          $conds[] = array(0);
          $conds[] = array($tmpDeal[0]['deal_id']);
          $query = $db->db_select("bill", $cols, $conds);
          $queries[] .= $query.";";
          $result = $db->database->query($query);
          $tmpBill = $result->fetchAll(SQLITE_ASSOC);
        }        

        //Get Address
        $cols = NULL;
        $conds = array();
        $conds[] = array(0);
        $conds[] = array(0);
        $conds[] = array($tmpDeal[0]['address_id']);
        $query = $db->db_select("address", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpAddr = $result->fetchAll(SQLITE_ASSOC);

        //Get Postage
        $cols = NULL;
        $conds = array();
        $conds[] = array(1);
        $conds[] = array(0);
        $conds[] = array($tmpAddr[0]['address_country']);
        $query = $db->db_select("postage", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpPostg = $result->fetchAll(SQLITE_ASSOC);

        //Get Product2Deal
        $cols = NULL;
        $conds = array();
        $conds[] = array(2);
        $conds[] = array(0);
        $conds[] = array($tmpDeal[0]['deal_id']);
        $query = $db->db_select("product2deal", $cols, $conds);
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpProd2Deal = $result->fetchAll(SQLITE_ASSOC);

        //Construct Temp BAG
        $prodIds = array();
        foreach ($tmpProd2Deal as $value) {
          $prodIds[] = $value["product_id"];
        }
        sort($prodIds);
        $prodIdsUnique = array_unique($prodIds);
        $prodIdsCount = array_count_values($prodIds);

        //Construct Temp PRODUCT
        $prodIdsUniqueStr = implode(",", $prodIdsUnique);
        $query = "SELECT * FROM product WHERE product_id in (".$prodIdsUniqueStr.")";
        $queries[] .= $query.";";
        $result = $db->database->query($query);
        $tmpProd = $result->fetchAll(SQLITE_ASSOC);
/*
        echo "<br />";
        echo "<pre>";
        print_r($queries);
        print_r($tmpDeal);
        print_r($tmpCust);
        print_r($tmpAddr);
        print_r($tmpPostg);
        print_r($tmpProd2Deal);
        print_r($tmpProd);
        print_r($tmpBill);
        echo "</pre>";
*/
?>
          <div class="page-header">
            <h1><?php echo SHOPNAME; ?> <small>Transaction Status</small></h1>
          </div>
          <div class="row">
            <div class="span2">
              <span class="badge badge-info">Order Number</span><br /><?php echo $tmpBill[0]['bill_mol_oderid']; ?><br />
            </div>
            <div class="span2">
              &nbsp;
            </div>
            <div class="span2">
              <span class="badge badge-info">Order Date</span><br /><?php echo date("Y-m-d H:i:s", $tmpBill[0]['bill_timestamp']); ?> <?php echo printTimeZone(); ?><br />
            </div>
          </div>
          <hr />
          <?php printOrderTable($tmpAddr, $tmpCust, $tmpProd, $tmpPostg, $prodIdsCount); ?>
<?php
        $order_status = $tmpBill[0]['bill_status'];
        $order_status_label = array ( "new" => " label-info"
                                    , "pending" => " label-default"
                                    , "expired" => " label-important"
                                    , "canceled" => " label-important"
                                    , "completed" => " label-success" );
?>
          <dl class="dl-horizontal">
            <dt>Order Status</dt>
            <dd><small><span class="label<?php echo $order_status_label["$order_status"]; ?>"><?php echo $tmpBill[0]['bill_status']; ?></span><sup>1</sup></small></dd>
            <dt>Order Validity</dt>
            <dd><small><?php echo seconds_to_days($tmpBill[0]['bill_period']); ?></small></dd>
            <dt>Order Expiry</dt>
            <dd><small><?php echo date("Y-m-d H:i:s", ($tmpBill[0]['bill_timestamp'] + $tmpBill[0]['bill_period'])); ?> </small><?php echo printTimeZone(); ?></dd>
            <dt>Time now</dt>
            <dd><small><?php echo date("Y-m-d H:i:s", time()); ?> </small><?php echo printTimeZone(); ?></dd>
            <dt>Time left</dt>
            <?php if ($tmpBill[0]['bill_status'] == "new") { ?>
            <dd><small><?php echo seconds_to_days(($tmpBill[0]['bill_timestamp'] + $tmpBill[0]['bill_period']) - time()); ?></small></dd>
            <?php } else { ?>
            <dd><small class="muted"><?php echo seconds_to_days(($tmpBill[0]['bill_timestamp'] + $tmpBill[0]['bill_period']) - time()); ?></small></dd>
            <?php } ?>
          </dl>
<?php
        if (($tmpBill[0]['bill_status'] == "new") || ($tmpBill[0]['bill_status'] == "pending")){
?>
          <div class="row">
            <div class="span3">
              <div class="alert alert-info">
                <h4>Pay Online</h4>
                <p>&nbsp;</p>
                <p><?php echo printPayOnline($tmpBill[0]['bill_mol_url']); ?></p>
              </div>
            </div>
            <div class="span3">
              <div class="alert alert-info">
                <h4>Pay Offline</h4>
                <p>&nbsp;</p>
                <p><?php echo printPayOffline("#"); ?></p>
              </div>
            </div>
          </div>
<?php
        }
        if ($tmpBill[0]['bill_status'] == "expired") {
?>
          <div class="alert alert-error">
            <h4>Order has expired</h4>
            <p>&nbsp;</p>
            <p>
              <?php echo printContinueShopping("#"); ?>
            </p>
          </div>
<?php
        }
        if ($tmpBill[0]['bill_status'] == "canceled") {
?>
          <div class="alert alert-error">
            <h4>Order has been canceled</h4>
            <p>&nbsp;</p>
            <p>
              <?php echo printContinueShopping("#"); ?>
            </p>
          </div>
<?php
        }
        if ($tmpBill[0]['bill_status'] == "completed") {
?>
          <div class="alert alert-success">
            <h4>Order has been paid</h4>
            <p>&nbsp;</p>
            <p>
              <?php echo printContinueShopping("#"); ?>
            </p>
          </div>
<?php
        }
      } else {
        $error_msg = "E-mail error.";
        printAlert("error", "Oh snap!", $error_msg, "#");
        printContinueShopping("#");
      }
    } else {
      $error_msg = "Transaction error.";
      printAlert("error", "Oh snap!", $error_msg, "#");
      printContinueShopping("#");
    }
  } else {
    $error_msg = "Page error.";
    printAlert("error", "Oh snap!", $error_msg, "#");
    printContinueShopping("#");
  }
?>
        </div>
      </div>
<?php
//include page footer
footer_template();
?>